package com.woniu.cms.auth.core.controller;


import com.woniu.cms.auth.core.service.IRbacPermService;
import com.woniu.cms.auth.core.vo.CUPermVo;
import com.woniu.cms.auth.core.vo.PermVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lilili
 * @since 2022-09-16
 */
@RestController
@RequestMapping("/rbac-perm")
@Api(tags="权限控制器")
public class RbacPermController {

    @Resource
    private IRbacPermService permService;

    @ApiOperation("获取所有权限之递归")
    @GetMapping("/all")
    public Result<List<PermVo>> all() {
        List<PermVo> list = permService.all();
        return Result.common(list);
    }

    @ApiOperation("获取所有权限之for循环")
    @GetMapping("/all1")
    public Result<List<PermVo>> all1() {
        List<PermVo> list = permService.all1();
        return Result.common(list);
    }

    @ApiOperation("获取所有权限之map-for循环")
    @GetMapping("/all2")
    public Result<List<PermVo>> all2() {
        List<PermVo> list = permService.all2();
        return Result.common(list);
    }

    @ApiOperation("增加权限")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody CUPermVo PermVo) {
        boolean add = permService.addPerm(PermVo);
        return Result.common(add);
    }

    @ApiOperation("修改权限")
    @PostMapping("/update")
    public Result<Object> update(@RequestBody CUPermVo PermVo) {
        boolean update = permService.updatePerm(PermVo);
        return Result.common(update);
    }

    @ApiOperation("删除权限")
    @PostMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable("id") Integer id) {
        boolean b = permService.removeById(id);
        return Result.common(b);
    }




    @ApiOperation("展示角色权限")
    @GetMapping("/display/{roleId}")
    public Result<List<PermVo>> display(@PathVariable("roleId") Integer roleId) {
        List<PermVo> list = permService.display(roleId);
        return Result.common(list);
    }

    /**
     * 给角色分配权限
     *  假设权限是个树形
     *  点击对勾时，会将角色id和此时他所有的儿子节点的编号传到后端
     *
     * @param roleId  角色id
     * @param permIds 烫id
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation("给角色分配权限")
    @PostMapping("/doAllot/{roleId}")
    public Result<Object> doAllot(@PathVariable("roleId") Integer roleId, @RequestBody Integer[] permIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("roleId", roleId);
        map.put("permIds", permIds);
        boolean doAllot = permService.doAllot(map);

        return Result.common(doAllot);
    }

    @ApiOperation("给角色解除权限")
    @DeleteMapping("/doUnAllot/{roleId}")
    public Result<Object> doUnAllot(@PathVariable("roleId") Integer roleId, @RequestBody Integer[] permIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("roleId", roleId);
        map.put("permIds", permIds);
        boolean doAllot = permService.doUnAllot(map);

        return Result.common(doAllot);
    }



}

